package com.atguigu.edu.realtime.app.dwd.db;

import com.atguigu.edu.realtime.util.MyKafkaUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @ClassName: DwdTradeOrder
 * @Author: ghy
 * @Date: 2022/10/19 8:40
 * @Description:
 **/
public class DwdTradePreOrder {
    public static void main(String[] args) {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        //表执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        //失效时间
        tableEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(15*60 + 10));
        //检查点
        //2.1 开启检查点
//        env.enableCheckpointing(5000L, CheckpointingMode.EXACTLY_ONCE);
//        //2.2 设置检查点超时时间
//        env.getCheckpointConfig().setCheckpointTimeout(60000L);
//        //2.3 设置job取消之后，检查点是否保留
//        env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);
//        //2.4 设置两个检查点之间最小时间间隔
//        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(2000L);
//        //2.5 设置重启策略
//        env.setRestartStrategy(RestartStrategies.failureRateRestart(3, Time.days(30),Time.seconds(3)));
//        //2.6 设置状态后端
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop101:8020/gmall/ck");
//        //2.7 设置操作hadoop的用户
//        System.setProperty("HADOOP_USER_NAME","atguigu");
        //3.从kafka的topic_db主题中读取数据 创建动态表
        tableEnv.executeSql(MyKafkaUtil.getTopicDbDDL("dwd_trade_order_pre_group"));
        //4.过滤出订单明细
        Table orderDetail = tableEnv.sqlQuery("select \n" +
                "data['id'] id,\n" +
                "data['course_id'] course_id,\n" +
                "data['course_name'] course_name,\n" +
                "data['order_id'] order_id,\n" +
                "data['user_id'] user_id,\n" +
                "data['origin_amount'] origin_amount,\n" +
                "data['coupon_reduce'] coupon_reduce,\n" +
                "data['final_amount'] final_amount,\n" +
                "data['create_time'] create_time,\n" +
                "ts od_ts,\n" +
                "proc_time\n" +
                "from `topic_db` where `table` = 'order_detail' " +
                "and `type` = 'insert'\n");
        tableEnv.createTemporaryView("order_detail", orderDetail);
        //5.过滤订单表
        Table orderInfo = tableEnv.sqlQuery("select \n" +
                "data['id'] id,\n" +
                "data['order_status'] order_status,\n" +
                "data['session_id'] session_id,\n" +
                "data['province_id'] province_id,\n" +
                "data['update_time'] update_time,\n" +
                "`type`,\n" +
                "`old`,\n" +
                "ts oi_ts\n" +
                "from `topic_db`\n" +
                "where `table` = 'order_info'\n" +
                "and (`type` = 'insert' or `type` = 'update')");
        tableEnv.createTemporaryView("order_info", orderInfo);

        //6.日志数据
        tableEnv.executeSql("CREATE TABLE page_log (\n" +
                "    common map<string,string>,\n" +
                "    ts string\n" +
                ") " + MyKafkaUtil.getKafkaDDL("dwd_traffic_page_log", "dws_trade_order_pre_group"));

        //tableEnv.executeSql("select * from page_log").print();
        Table orderLog = tableEnv.sqlQuery("select \n" +
                "`common`['sc'] sc,\n" +
                "`common`['sid'] sid,\n"+
                "ts ol_ts\n" +
                "from `page_log`\n"
        );
        tableEnv.createTemporaryView("order_Log", orderLog);
//        tableEnv.executeSql("select * from order_Log").print();




        //7.关联
        Table resultTable = tableEnv.sqlQuery("select \n" +
                "od.id,\n" +
                "od.course_id,\n" +
                "od.course_name,\n" +
                "od.order_id,\n" +
                "od.user_id,\n" +
                "od.origin_amount,\n" +
                "od.coupon_reduce,\n" +
                "od.final_amount,\n" +
                "date_format(od.create_time, 'yyyy-MM-dd') date_id,\n" +
                "od.od_ts,\n" +


                "oi.order_status,\n" +
                "oi.session_id session_id,\n" +
                "oi.province_id,\n" +
                "date_format(oi.update_time, 'yyyy-MM-dd') update_date_id,\n" +
                "oi.`type`,\n" +
                "oi.`old`,\n" +
                "oi.oi_ts,\n" +

                "ol.sc,\n" +
                "ol.ol_ts,\n"+


                "current_row_timestamp() row_op_ts\n" +

                "from order_detail od \n" +
                "join order_info oi\n" +
                "on od.order_id = oi.id\n"+
                "join order_Log ol\n" +
                "on ol.sid = oi.session_id"
        );
        tableEnv.createTemporaryView("result_table", resultTable);
        //tableEnv.executeSql("select * from result_table").print();

        //8.创建动态表,映射
        tableEnv.executeSql("create table dwd_trade_pre_order(\n" +
                "id string,\n" +
                "course_id string,\n" +
                "course_name string,\n" +
                "order_id string,\n" +
                "user_id string,\n" +
                "origin_amount string,\n" +
                "coupon_reduce string,\n" +
                "final_amount string,\n" +
                "date_id string,\n" +
                "od_ts string,\n" +

                "order_status string,\n" +
                "session_id string,\n" +
                "province_id string,\n" +
                "update_date_id string,\n" +
                "`type` string,\n" +
                "`old` map<string,string>,\n" +
                "oi_ts string,\n" +

                "sc string,\n" +
                "ol_ts string,\n"+

                "row_op_ts timestamp_ltz(3),\n" +
                "primary key(id) not enforced\n" +
                ")" + MyKafkaUtil.getUpsertKafkaDDL("dwd_trade_pre_order"));

        //9.写入
        tableEnv.executeSql("insert into dwd_trade_pre_order select * from result_table");
    }
}
